
// 右键菜单管理
var parent = chrome.contextMenus.create({ "title": "收藏此页面" });
var child1 = chrome.contextMenus.create(
    { "title": "添加到工作", "parentId": parent, "onclick": addToWork });
var child2 = chrome.contextMenus.create(
    { "title": "添加到生活", "parentId": parent, "onclick": addToLive });
var child3 = chrome.contextMenus.create(
    { "title": "添加到临时", "parentId": parent, "onclick": addToShort });
// chrome.contextMenus.create(
//     { "title": "查看存储的数据", "parentId": parent, "onclick": seleteData });
chrome.contextMenus.create(
    { "title": "修复数据", "parentId": parent, "onclick": deleerr });

function seleteData() {
    chrome.storage.local.get(['mRet'], res => {
        console.log("当前数据-->" + JSON.stringify(res.mRet))
    })
}

function addToWork(info, tab) {
    addStorage(tab, 0)
}
function addToLive(info, tab) {
    addStorage(tab, 1)
}
function addToShort(info, tab) {
    addStorage(tab, 2)
}

/**
 * 添加数据 根据类型添加到不同分类中，默认添加到“short”
 * @param { {"title": "我试试啊", "favIconUrl": "http：icon", "url": "http：url"} } tab 对象
 * @param {number} type 类型 0-->work；1-->live；2-->short； 
 */
function addStorage(tab, type) {
    console.log("this add")

    var mRet = { "work": [], "live": [], "short": [] }
    var item = { "title": "", "icon": "", "url": "" }
    var workItmes = [], liveItmes = [], shortItmes = []
    //先查询之前的数据，在拼上现在的数据
    chrome.storage.local.get(['mRet'], res => {
        // console.log("res-->" + JSON.stringify(res.mRet))
        if (res.mRet) {
            workItmes = res.mRet.work
            liveItmes = res.mRet.live
            shortItmes = res.mRet.short

            item["title"] = tab.title
            item["icon"] = tab.favIconUrl ? tab.favIconUrl : "icon/erha.jpg"
            item["url"] = tab.url

            if (type == 0) {
                workItmes.push(item)
            } else if (type == 1) {
                liveItmes.push(item)
            } else if (type == 2) {
                shortItmes.push(item)
            }

            mRet["work"] = workItmes
            mRet["live"] = liveItmes
            mRet["short"] = shortItmes
            console.log(type + mRet["short"]);
        } else {
            //先存入空值，在存入数据
            chrome.storage.local.set({ mRet: mRet }, function () {
                console.log('空值--》保存成功！');
            });
            addStorage(tab, type)//默认添加到“short”
        }

        chrome.storage.local.set({ mRet: mRet }, function () {
            console.log('保存成功！xia');
            // console.log(mRet);
        });
    })

}

/**
 * 删除某个数据
 * @param {number} index 索引
 * @param {number} type  类型 0-->work；1-->live；2-->short； 
 */
function delStorage(index, type) {
    var mRet = { "work": [], "live": [], "short": [] }
    var item = { "title": "", "icon": "", "url": "" }
    var items = [], workItmes = [], liveItmes = [], shortItmes = []
    //先查询之前的数据
    chrome.storage.local.get(['mRet'], res => {
        // console.log("res-->" + JSON.stringify(res.mRet))

        workItmes = res.mRet.work
        liveItmes = res.mRet.live
        shortItmes = res.mRet.short

        if (type == 0) {
            workItmes.splice(index, 1)
        } else if (type == 1) {
            liveItmes.splice(index, 1)
        } else if (type == 2) {
            shortItmes.splice(index, 1)
        }

        mRet["work"] = workItmes
        mRet["live"] = liveItmes
        mRet["short"] = shortItmes

        chrome.storage.local.set({ mRet: mRet }, function () {
            console.log('保存成功！');
        });
    })
}

/**
 * 数据排序
 * 删除拖动的item，并插入到目标位置
 * @param {*} type 类型(哪个分组) 0-->work；1-->live；2-->short； 
 * @param {*} startIndex 起始索引
 * @param {*} endIndex 目标索引
 */
function replaceStorage(type, startIndex, endIndex, callback) {

    var mRet = { "work": [], "live": [], "short": [] }
    var item = { "title": "", "icon": "", "url": "" }
    var items = [], workItmes = [], liveItmes = [], shortItmes = []
    //先查询之前的数据
    chrome.storage.local.get(['mRet'], res => {
        // console.log("res-->" + JSON.stringify(res.mRet))

        workItmes = res.mRet.work
        liveItmes = res.mRet.live
        shortItmes = res.mRet.short

        if (type == 0) {
            var dragData = workItmes[startIndex]

            workItmes.splice(startIndex, 1)
            workItmes.splice(endIndex, 0, dragData)

        } else if (type == 1) {
            var dragData = liveItmes[startIndex]

            liveItmes.splice(startIndex, 1)
            liveItmes.splice(endIndex, 0, dragData)
        } else if (type == 2) {
            var dragData = shortItmes[startIndex]

            shortItmes.splice(startIndex, 1)
            shortItmes.splice(endIndex, 0, dragData)

        }

        mRet["work"] = workItmes
        mRet["live"] = liveItmes
        mRet["short"] = shortItmes

        chrome.storage.local.set({ mRet: mRet }, function () {
            console.log('保存成功！');
            callback(mRet)
        });
    })
}

function deleerr() {
    var mRet = { "work": [], "live": [], "short": [] }
    var item = { "title": "", "icon": "", "url": "" }
    var items = [], workItmes = [], liveItmes = [], shortItmes = []
    //先查询之前的数据
    chrome.storage.local.get(['mRet'], res => {

        workItmes = res.mRet.work
        liveItmes = res.mRet.live
        shortItmes = res.mRet.short

        for (i = 0; i < workItmes.length; i++) {
            if (!workItmes[i]) {
                workItmes.splice(i, 1)
            }
        }
        for (i = 0; i < liveItmes.length; i++) {
            if (!liveItmes[i]) {
                liveItmes.splice(i, 1)
            }
        }
        for (i = 0; i < shortItmes.length; i++) {
            if (!shortItmes[i]) {
                shortItmes.splice(i, 1)
            }
        }

        mRet["work"] = workItmes
        mRet["live"] = liveItmes
        mRet["short"] = shortItmes

        chrome.storage.local.set({ mRet: mRet }, function () {
            console.log('保存成功！');
        });
    })
}